package com.xpgk.mvc.shopProfitQuery;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;

@Service(name = ShopProfitQueryService.serviceName)
public class ShopProfitQueryService extends  BaseService {
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(ShopProfitQueryService.class);
	public static final String serviceName = "shopProfitQueryService";
	public static final String shopprofitquery_sales = "sys.shopprofitquery.sales";
	public static final String shopprofitquery_charge = "sys.shopprofitquery.charge";
	
	public List<Record> queryPrice(String organIds,String minTime,String maxTime){
		//收入
		String sql = getSqlMy(shopprofitquery_sales);
		List<Record> result = Db.find(sql, organIds, organIds, minTime, maxTime);
		int count = result.size();
		Double sumshouruzjcostprofit = 0.0;
		if(count>0){	
			Double sumj = 0.0;
			for(Record record : result){
				sumj = sumj +(Double)record.get("shouruzj");
			}
			Record paramE = new Record().set("chanplx","合计").set("shouruzj",sumj+"元");
			result.add(paramE);
			for(Record r : result){
				r.set("project", "收入");
			}
			//成本
			ArrayList<Record> list = new ArrayList<Record>();
			Double sumshouruzjcost=0.0;
			for(int i=0;i<result.size()-1;i++){
				Object project = result.get(i).get("project");
				Object chanplx = result.get(i).get("chanplx");
				Object chanpmc = result.get(i).get("chanpmc");
				Object chanpsl = result.get(i).get("chanpsl");
				Object chanpjj = result.get(i).get("chanpjj");
				Object shouruzjcost = result.get(i).get("shouruzjcost");
				Record costResult = new Record().set("project","成本").set("chanplx",chanplx).set("chanpmc",chanpmc).set("chanpsl",chanpsl).set("danj",chanpjj+"元").set("shouruzj",shouruzjcost+"元");
				list.add(costResult);
				sumshouruzjcost = sumshouruzjcost + (Double)shouruzjcost;
			}
			result.addAll(list);
			Record heji = new Record().set("chanplx","合计").set("shouruzj",sumshouruzjcost+"元").set("project","成本");
			result.add(heji);
			//毛利
			ArrayList<Record> profit = new ArrayList<Record>();
			for(int i=0;i<count;i++){
				Object project = result.get(i).get("project");
				Object chanplx = result.get(i).get("chanplx");
				Object chanpmc = result.get(i).get("chanpmc");
				Object chanpsl = result.get(i).get("chanpsl");
				Object profit2 = result.get(i).get("profit");
				Object shouruzjcostprofit = result.get(i).get("shouruzjcostprofit");
				sumshouruzjcostprofit = sumshouruzjcostprofit + (Double) shouruzjcostprofit;
				Record costResult = new Record().set("project","毛利").set("chanplx",chanplx).set("chanpmc",chanpmc).set("chanpsl",chanpsl).set("danj",profit2+"元").set("shouruzj",shouruzjcostprofit+"元");
				profit.add(costResult);
			}
			result.addAll(profit);
			Record hejiprofit = new Record().set("chanplx","合计").set("shouruzj",sumshouruzjcostprofit+"元").set("project","毛利");
			result.add(hejiprofit);
		}
		//费用
		String sqlcharge = getSqlMy(shopprofitquery_charge);
		List<Record> chargeResult = Db.find(sqlcharge, organIds, minTime, maxTime);
		Double sumcharg = 0.0;
		Double lirun1=0.0;
		if(chargeResult.size()>0){
			int chargsize = chargeResult.size();
			for(Record charg : chargeResult){
				charg.set("shouruzj", charg.get("price")+"元").set("chanpmc",charg.get("type")).set("project","费用");
				Object chargprice = charg.get("price");
				sumcharg = sumcharg + (Double) chargprice; 
			}
			result.addAll(chargeResult);
			Record chargheji = new Record().set("chanplx","合计").set("project","费用").set("shouruzj",sumcharg+"元");
			result.add(chargheji);
			
			lirun1 = sumshouruzjcostprofit-sumcharg;
		}
		Record lirun = new Record().set("project","利润").set("shouruzj", lirun1+"元").set("chanplx","-");
		result.add(lirun);
//==========================收入格式=============================================		
		for(int i=0;i<count;i++){
			Record record = result.get(i).set("danj",result.get(i).get("danj")+"元").set("shouruzj", result.get(i).get("shouruzj")+"元");
		}		
		return result;
	}
	
	public Integer resultsize(String organIds,String minTime,String maxTime){
		String sql = getSqlMy(shopprofitquery_sales);
		List<Record> result = Db.find(sql, organIds, organIds, minTime, maxTime);
		return result.size();
	}
	public Integer chargesize(String organIds, String minTime, String maxTime) {
		String sqlcharge = getSqlMy(shopprofitquery_charge);
		List<Record> chargeResult = Db.find(sqlcharge, organIds, minTime, maxTime);
		return chargeResult.size();
	}
}
